<?php
require_once '/mnt/default/crontabscript/examples/EasyMySQLi.inc.php';
$db = new EasyMySQLi('localhost', 'root', 'root', 'madv_db1');
try {
  $result=$db->queryAllRows('select createtime,filename,sortorder from file WHERE (appconfirm = 1 or serverconfirm = 1) and ((type=1 and status = 2) or type=0) order by createtime desc');
  if(!empty($result)){
    $click=array();
    $tmp=array();
    include '/mnt/default/api.madv360.com/lib/redis.php';
    $redis->select(0);
    $first_day_of_week=0;
    $first_day_of_month=0;
    $w=-1;
    for($i=0;$i<count($result);$i++){
      if(!empty($result[$i]["filename"]) && !empty($result[$i]["createtime"])){
        //echo $result[$i]["filename"];exit;
        if($result[$i]["sortorder"]<0 && (abs($result[$i]["sortorder"])+5*60)<=time()){
          echo 1;
          continue;
        }
        if($i==0){
          $first_day_of_week=getweek(date('Y-m-d',$result[$i]["createtime"]));
          $first_day_of_month=getmonth(date('Y-m-d',$result[$i]["createtime"]));
          //echo $first_day_of_week."~".$first_day_of_month;
        }
        $no=$redis->get($result[$i]["filename"]."_madvsv1_click");
        $no=empty($no)?0:$no;
        //先从月开始
        if($result[$i]["createtime"]>$first_day_of_month){
        }else{
          $first_day_of_month=getmonth(date('Y-m-d',$result[$i]["createtime"]));
          $w++;
          foreach ($tmp as $key=>$value){
            $click[$w][$key]=$value;
          }
          unset($tmp);
          //print_r($click);
        }
        if($no<=10){
          $tmp[$result[$i]["filename"]]=$no;
        }else{
          if($result[$i]["createtime"]>$first_day_of_week){
            echo $result[$i]["createtime"];
            $click[$w][$result[$i]["filename"]]=$no;
          }else{
            $first_day_of_week=getweek(date('Y-m-d',$result[$i]["createtime"]));
            $w++;
            $click[$w][$result[$i]["filename"]]=$no;
          }
        }
      }
      //echo $result[$i]["createtime"];
      //echo strtotime('1970-01-01 19:25:43');
    }
    $w++;
    foreach ($tmp as $key=>$value){
      $click[$w][$key]=$value;
    }
    unset($tmp);
    //print_r($click);
    $j=0;
    for($i=0;$i<count($click);$i++){
      arsort($click[$i]);
      foreach($click[$i] as $key=>$value){
        $j++;
        $result=$db->queryNoResult('update file set sortorder=? WHERE filename=?',$j,$key); 
      }
      //$click[$i]=rsort($tmp);
    }
    print_r($click);
  }
}catch (MySQLiQueryException $ex) {
  //echo '{"rval":3}';// 失败
}
function getweek($time){
  $date=new DateTime($time);
  $date->modify('this week');
  $first_day_of_week=$date->format('U');
  //$date->modify('this week +7 days');
  //$end_day_of_week=$date->format('Y-m-d');
  return $first_day_of_week;
  //echo $end_day_of_week;
}
function getmonth($date){
  $firstday = strtotime(date('Y-m-01', strtotime($date)));
  return $firstday;
  //$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  //return array($firstday,$lastday);
}
